﻿Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Imports GenDotNet.TemplateSupport
Imports GenDotNet.TemplateSupport.VB
Imports GenDotNet.Task.Common
Imports GenDotNet.ObjectModel
Imports GenDotNet.TemplateSupport.Strings
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">
Imports <xmlns="http://kadgen.com/tempCode.xsd">

Public Class genBizObjectTemplate
   Inherits DotNetBase


#Region "Overriding methods"

   Public Overrides ReadOnly Property Id() As System.Guid
      Get
         Return New Guid("{2319542C-4ABC-4661-B58C-29E3BC6F4717}")
      End Get
   End Property

   Protected Overrides Function GetFileImports() As GenDotNet.TemplateSupport.ImportInfoCollection
      ' Add standard imports in the ImportInfoCollection constructor, 
      ' or here using the Add methods of the ImportInfoCollection 
      ' base class. You should rarely pass string.
      Return New ImportInfoCollection
   End Function

   Protected Overrides Function GetFileName(ByVal current As GenDotNet.Model.Common.IBaseData) As String
      ' NEWTEMPLATE: Update the following file name
      Dim currentObjectData = CType(current, IObjectData)
      Return OutputFileName(GetClassName(currentObjectData))
   End Function

#End Region

   Private Function GetClassName(ByVal objectData As IObjectData) As String
      Return NamingFactory.GetGeneratedName(objectData.ClassName)
   End Function


   Protected Overrides Function GenerateContents() As String
      ' The class info takes care of a lot of the grimy formatting and avoids typos
      ' use these info classes where practical for clarity and ease of use
      Dim classInfo = New TypeInfo(GetClassName(ObjectData), _
                             Scope.Public, _
                             TypeCategory.Class, _
                             TypeModifiers.MustInherit)

      classInfo.InheritsFrom = New TypeInfo(Symbols.Type.BusinessObjectBase, _
                                            ObjectData.ClassName, _
                                            ObjectData.PrimaryKey.NetType.Output)

      classInfo.Attributes.AddSerializable()
      Return OutputClass(classInfo, Function() _
<code>
   <%= If(ObjectData.Children.Count = 0, String.Empty, _
      OutputField(Symbols.Field.ChildrenAreLoaded, StandardNetType.Uint64)) %>
   <%= MemberConstructor() %>
   <%= RegionBusinessMethods() %>
</code>.Value)

   End Function

   Protected Function MemberConstructor() As String
      Return OutputConstructor(Scope.Private, Function() String.Empty)
   End Function


#Region "BusinessMethods"
   Protected Function RegionBusinessMethods() As String
      Return OutputRegion("Business Methods", Function() _
<code>
   <%= From prop In ObjectData.Properties _
      Select OutputField("m" + prop.Name, prop.NetType) %>

   <%= From child In ObjectData.Children _
      Select OutputField("m" & GetChildTypeName(child), _
      child.ObjectData.ClassName, _
      GetChildNewMethodName(child)) %>

   <%= From prop In ObjectData.Properties _
      Select MemberPropertyAccess(prop) %>

   <%= From child In ObjectData.Children _
      Select MemberChildAccess(child) %>

   <%= MemberIsValid() %>
   <%= MemberIsDirty() %>
   <%= MemberGetIDValue() %>
   <%= MemberGetPrimaryKey() %>
</code>.Value)

   End Function

   Private Function MemberPropertyAccess(ByVal prop As IPropertyData) As String
      Dim propInfo = MemberPropertyInfo(prop, CurrentTypeInfo)
      Return propInfo.Output()
   End Function

   Private Function MemberChildAccess(ByVal child As IChildData) As String
      Return OutputProperty(child.PluralUniqueName, Scope.Public, Nothing, GetChildTypeName(child), True, _
                            Function() _
<code>
   If (Me.childrenAreLoaded And <%= child.ChildFlag %>) = 0 Then
      GetItemChildren(<%= meString %>.GetPrimaryKey())
   End If
   Return m<%= child.PluralUniqueName %>
</code>.Value, Nothing)
   End Function

   Private Function MemberIsValid() As String
      Return OutputFunction(Symbols.Method.IsValid, Scope.Public, MemberModifiers.Overrides, _
      StandardNetType.Boolean, Function() MemberBasePlusChildren(Symbols.Method.IsValid, True))
   End Function

   Private Function MemberIsDirty() As String
      Return OutputFunction(Symbols.Method.IsDirty, Scope.Public, MemberModifiers.Overrides, _
      StandardNetType.Boolean, Function() MemberBasePlusChildren(Symbols.Method.IsDirty, False))
   End Function

   Private Function MemberBasePlusChildren(ByVal methodName As String, ByVal UseAnd As Boolean) As String
      Dim separator = If(UseAnd, andAlsoString, orElseString).WithSpaceAround
      Return _
<code>
   Return MyBase.<%= methodName %>
   <%= (From child In ObjectData.Children Select _
      <code>
         <%= child.PluralUniqueName %>.<%= methodName %>
      </code>).ConcatenateWith(separator) %>
</code>.Value
   End Function

   Private Function MemberGetIDValue() As String
      Return OutputFunction(Symbols.Method.GetIDValue, Scope.Protected, MemberModifiers.Overrides, _
      StandardNetType.Object, Function() _
<code>
   Return m<%= ObjectData.PrimaryKeys(0).Name %>
</code>.Value)
   End Function

   Private Function MemberGetPrimaryKey() As String
      Return OutputFunction(Symbols.Method.GetPrimaryKey, _
                            Scope.Protected, _
                            MemberModifiers.Overrides, _
                            ObjectData.PrimaryKey.NetType, _
Function() _
<code>
   Return m<%= ObjectData.PrimaryKeys(0).Name %>
</code>.Value)
   End Function

#End Region


End Class
